import Vue from 'vue'
import Vuex from 'vuex'
import {
  getToken,
  setToken,
  delToken,
  getRefreshToken,
  setRefreshToken,
  delRefreshToken
} from '@/utils/storage.js'
import channel from './modules/channel.js'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    refreshToken: getRefreshToken(),
    token: getToken()
  },
  mutations: {
    setRefreshToken (state, newRefreshToken) {
      state.refreshToken = newRefreshToken
      // 如果设置的是一个无效的token，那么就直接将本地存储删掉
      if (!newRefreshToken) delRefreshToken()
      else setRefreshToken(newRefreshToken)
    },
    setToken (state, newToken) {
      state.token = newToken
      // 如果设置的是一个无效的token，那么就直接将本地存储删掉
      if (!newToken) delToken()
      else setToken(newToken)
    }
  },
  modules: {
    channel
  }
})

export default store
